<template>
  <el-dialog
    :title="title"
    v-model="visible"
    width="500px"
    append-to-body
    destroy-on-close
	 :close-on-click-modal="false"
  >
    <el-form
      ref="formRef"
      :model="formData"
      :rules="rules"
      label-width="100px"
    >
      <el-form-item label="端口名称" prop="name">
        <el-input v-model="formData.name" placeholder="请输入端口名称" />
      </el-form-item>
	  
      <el-form-item label="端口类型" prop="type">
		<Dict v-model="formData.type" code="blueprint_node_port_type" style="width: 100%;" />
      </el-form-item>
	  
	  <el-form-item prop="click">
	      <el-radio-group v-model="formData.click">
	  	  <el-radio :value="'1'">点击</el-radio>
	  	  <el-radio :value="'0'">无</el-radio>
	      </el-radio-group>
	  </el-form-item>
	  
	  <el-form-item label="字段类型" prop="fieldType" v-if="['field','select','array'].includes(formData.type)">
	  		<Dict v-model="formData.fieldType" code="blueprint_node_field_type" style="width: 100%;" />
	  </el-form-item>
	  
<!--      <el-form-item label="端口默认值" prop="value" v-if="['string','float', 'int' ,'boolean', 'double'].includes(formData.type)">
        <el-input v-model="formData.value" placeholder="请输入端口默认值"  />
      </el-form-item> -->
	  
    </el-form>
    <template #footer>
      <div class="dialog-footer">
        <el-button type="primary" @click="submitForm">确 定</el-button>
        <el-button @click="visible = false">取 消</el-button>
      </div>
    </template>
  </el-dialog>
</template>

<script setup lang="ts">
import { ref, reactive, watch } from 'vue';
import type { FormInstance } from 'element-plus';

interface ParamForm {
  id: string;
  name: string;
  code: string;
  type: string;
  value: string;
  click:string;
  fieldType:string;
}

const props = defineProps({
  modelValue: {
    type: Boolean,
    required: true
  },
  title: {
    type: String,
    required: true
  },
  param: {
    type: Object as () => ParamForm,
    required: true
  }
});

const emit = defineEmits(['update:modelValue', 'success']);

const visible = ref(false);
const formRef = ref<FormInstance>();
const formData = reactive<ParamForm>({
  id: '',
  name: '',
  code: '',
  type: '',
  value: '',
  click:'0',
  fieldType:'',
});

const rules = {
  id: [{ required: true, message: '请输入端口ID', trigger: 'blur' }],
  code: [{ required: true, message: '请输入端口标识', trigger: 'blur' }],
  type: [{ required: true, message: '请选择端口类型', trigger: 'change' }],
  click: [{ required: true, message: '请选择端口是否可以点击', trigger: 'change' }],
  fieldType: [{ required: true, message: '请选择字段类型', trigger: 'change' }],
};

watch(() => props.modelValue, (val) => {
  visible.value = val;
});

watch(() => visible.value, (val) => {
  emit('update:modelValue', val);
});

watch(
  () => props.param,
  (val) => {
    Object.assign(formData, val);
  },
  { deep: true }
);

const submitForm = async () => {
  if (!formRef.value) return;
  await formRef.value.validate();
  emit('success', { ...formData });
  visible.value = false;
};
</script>

<style lang="scss" scoped>
.dialog-footer {
  text-align: right;
}
</style> 